package com.sy.core.web;

import com.sy.core.model.SysUser;
import com.sy.core.service.*;
import com.sy.core.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import sun.security.util.Password;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 基础控制器
 * @Author :Chong.CaiXin
 * @Description :
 * @Date : Created in 15:53 2019/6/4
 */
public class BaseCtl {

    protected final Logger logger = LoggerFactory.getLogger(BaseCtl.class);

    protected SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    protected SimpleDateFormat dfs2 = new SimpleDateFormat("yyyy-MM-dd ");

    protected SimpleDateFormat dfs3 = new SimpleDateFormat("yyyy-MM ");

    protected SimpleDateFormat dfs4 = new SimpleDateFormat("yyyy");

    @Resource
    protected RedisTemplate<Object, Object> template;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected  HttpServletResponse response;

    @Autowired
    protected ProductTypeService productTypeService;

    @Autowired
    protected ProductService productService;
    @Autowired
    protected ProductAttributeService productAttributeService;
    @Autowired
    protected ProductAttributeValueService productAttributeValueService;
    @Autowired
    protected ProductSkuService productSkuService;
    @Autowired
    protected BannerService bannerService;
    @Autowired
    protected SysUserService sysUserService;
    @Autowired
    protected OrderService orderService;
    @Autowired
    protected OrderInfoDetailService orderInfoDetailService;
    @Autowired
    protected AddressService addressService;
    @Autowired
    protected ShoppingCartService shoppingCartService;
    @Autowired
    protected SolrClient solrClient;
    @Autowired
    protected ArticleService articleService;
    /**
     * 生成一個不重複的主鍵PK
     * @return
     */
    public  String getPrimaryKey(){
        return new StringBuffer().append(BaseUUID.get()).toString();
    }

    /**
     * 获取时间
     */
    public String getTime(){
        return dfs.format(new Date());
    }

    /**
     * 获取登录用户PK
     */
    public String getLoginUserPk() {
        return new CookUtils().getLoginUserId(request);
    }

    /**
     * 获取登录用户信息
     *
     * @return
     */
    public SysUser getLoginUser() {
        String userId = new CookUtils().getLoginUserId(request);
        if (StringUtils.isNotBlank(userId)) {
            return RedisUtil.getFormRedis(template, userId);
        }
        return null;
    }
}
